var planID = getQueryString("planID");
var moduleCode = getQueryString("moduleCode");
var moduleType = getQueryString("moduleType");
$(function () {
    $("#jqGrid").Grid({
        url: '../modulemaintainoptions/list?isEffect=1&moduleType='+moduleType,
        rownumWidth: 35,
        colModel: [
            {label: '序号', name: 'orNum', index: 'OR_NUM', width: 40},
            {label: '一级名称', name: 'optionSubclass', index: 'optionSubclass', width: 60},
            {label: '二级名称', name: 'optionName', index: 'OPTION_NAME', width: 60},
            {label: '方法手段', name: 'method', index: 'METHOD', width: 80},
            {label: '实施频率', name: 'implPre', index: 'IMPL_PRE', width: 60,
                formatter: function (value) {
                    if(value==null||value==""){
                        return "暂无"
                    }else{
                        return sysEnums.FRERATE[value];
                    }
                }
            },
            {label: '检查标准', name: 'inspectStandard', index: 'INSPECT_STANDARD'},
            {label: '选项备注', name: 'optionRemark', index: 'OPTION_REMARK', width: 80},
            {label: '记录', name: 'rocords', index: 'ROCORDS', width: 80},
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
        ]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        moduleMaintainOptions: {
            updateDate: '',
            updateUser: '',
            createDate: '',
            createUser: '',
            delFlag: '',
            rocords: '',
            noProductDate: '',
            productDate: '',
            optionRemark: '',
            inspectStandard: '',
            method: '',
            optionSubclass: '',
            optionType: '',
            optionName: '',
            id: '',
            planId: ''
        },
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: '',
            optionType: '',
            method: ''
        },
        implPreList:enumsToList("FRERATE2"),
    },
    methods: {
        query: function () {
            vm.reload();
        },
        //保存数据到维护规程选项关联表数据
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.moduleMaintainOptions = {
                updateDate: '',
                updateUser: '',
                createDate: '',
                createUser: '',
                delFlag: '',
                rocords: '',
                noProductDate: '',
                productDate: '',
                optionRemark: '',
                inspectStandard: '',
                method: '',
                optionSubclass: '',
                optionType: '',
                optionName: '',
                planId: id
            };
        },
        update: function (event) {
            var ids = getSelectedRows("#jqGrid");
            // console.log("e",ids);
            var nowIds = "";
            var num = ids.length;
            if (ids.length > 0) {
                for (var i = 0; i < ids.length; i++) {
                    var nowId = ids[i];

                    nowIds += nowId;
                    if (i < ids.length) {
                        nowIds += ",";
                    }
                }
                var params = {"planID": planID, "opIDS": nowIds, "num": num,'moduleCode':moduleCode};
                console.log("......", params);
                //执行保存函数
                /*Ajax.request({
                    url: "../modulemaintainrelate/saveWhgc",
                    params: params,
                    type: "POST",
                    contentType: "json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });*/

                $.post("../modulemaintainrelate/savePlan", params, function (data) {
                    var code = data.code;
                    var msg = data.msg;
                    if (code == 0) {
                        alert(msg, function (index) {
                            //获取父窗口的值，进行关闭
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        });

                        // layer.closeAll();
                    } else {
                        alert(msg);
                    }
                });

            } else {
                iview.Message.error("请选择一条记录");
                return;
            }


            // if (id == null) {
            //     return;
            // }
            // vm.showList = false;
            // vm.title = "修改";
            //
            // vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var url = vm.moduleMaintainOptions.id == null ? "../modulemaintainoptions/save" : "../modulemaintainoptions/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.moduleMaintainOptions),
                type: "POST",
                //contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        //执行最近一次记录
        doOldChose: function (event) {

            Ajax.request({
                url: "../modulemaintainoptions/doOldChose/" + planID,
                // params: JSON.stringify(whgcID),
                type: "POST",
                // contentType: "application/json",
                successCallback: function () {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../modulemaintainoptions/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../modulemaintainoptions/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.moduleMaintainOptions = r.moduleMaintainOptions;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name, "optionType": vm.q.optionType, "method": vm.q.method},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: '',
                optionType: '',
                method: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});